﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace XFFSM
{
    internal class DelayTools : MonoBehaviour
    {

        private static DelayTools _instance;

        private static DelayTools Instance
        {
            get {

                if (_instance == null && Application.isPlaying)
                {
                    GameObject obj = new GameObject("XFFSMDelayTools");
                    _instance = obj.AddComponent<DelayTools>();
                    GameObject.DontDestroyOnLoad(obj);
                }

                return _instance;
            }
        }

        internal static void  DelayCall(Action action) 
        {
            if (Instance == null) return;
            Instance.StartCoroutine(DelayCallExcute(action));
        }

        private static IEnumerator DelayCallExcute(Action action)
        {
            yield return null;
            action?.Invoke();
        }
        
    }

}

